home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 32-bit_x86 / Russian / cpsimage.cab / data / xps / xmetWriteXML.proc < prev    next >
Text File  |  2009-03-16  |  17KB  |  473 lines

  1. #load "xipProcs/xipflatten.proc";
  2. #load "sys/transform2d.elf";
  3.  
  4. /* @WriteContentTypes
  5.   Support procedure to write XPS XML support files
  6.   Takes N-layer image and filename (no path or extension) and creates
  7.   four XML support files;
  8.      page1.xaml, 
  9.      page1.xaml.rels, 
  10.      page1.xaml, 
  11.      .rels, 
  12. */
  13.  
  14. private
  15. PROCEDURE WriteContentTypes (STRING dir, INTEGER page)
  16. {
  17.    STRING str = 
  18.    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  19.    "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\n" +
  20.    "<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>\n" +
  21.    "<Default Extension=\"fdseq\" ContentType=\"application/vnd.ms-package.xps-fixeddocumentsequence+xml\"/>\n" +
  22.    "<Default Extension=\"xml\" ContentType=\"application/xml\"/>\n" +
  23.    "<Default Extension=\"fdoc\" ContentType=\"application/vnd.ms-package.xps-fixeddocument+xml\"/>\n" +
  24.    "<Default Extension=\"fpage\" ContentType=\"application/vnd.ms-package.xps-fixedpage+xml\"/>\n" +
  25.    "<Default Extension=\"odttf\" ContentType=\"application/vnd.ms-package.obfuscated-opentype\"/>\n" +
  26.    "<Default Extension=\"ttf\" ContentType=\"application/vnd.ms-opentype\"/>\n" +
  27.    "<Default Extension=\"jpg\" ContentType=\"image/jpeg\"/>\n" +
  28.    "<Default Extension=\"png\" ContentType=\"image/png\"/>\n" +
  29.    "<Default Extension=\"tif\" ContentType=\"image/tiff\"/>\n" +
  30.    "<Override PartName=\"/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/>\n" +
  31.    "</Types>\n";
  32.  
  33.   str.Write (filename: dir + "/[Content_Types].xml", value: 1); 
  34. }
  35.  
  36. private
  37. PROCEDURE WriteDocSeq (STRING dir)
  38. {
  39.   STRING str = 
  40.    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  41.    "<FixedDocumentSequence xmlns=\"http://schemas.microsoft.com/xps/2005/06\">\n" +
  42.    "<DocumentReference Source=\"/FixedDocument.fdoc\"/>\n" +
  43.    "</FixedDocumentSequence>\n";
  44.   
  45.   str.Write (filename: dir + "/FixedDocumentSequence.fdseq", value:1);
  46. }
  47.  
  48. private
  49. PROCEDURE WriteFixedDocHdr (STRING dir, INTEGER page)
  50. {
  51.   STRING str = 
  52.    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  53.    "<FixedDocument xmlns=\"http://schemas.microsoft.com/xps/2005/06\">\n";
  54.  
  55.   str.Write (filename: dir + "/FixedDocument.fdoc", value:1);
  56. }
  57.  
  58. private
  59. PROCEDURE WriteFixedDoc (STRING dir, INTEGER page)
  60. {
  61.   STRING str =
  62.       "<PageContent Source=\"/" + page + ".fpage\"/>\n";
  63.  
  64.   str.Write (filename: dir + "/FixedDocument.fdoc", mode:"a", value:1);
  65. }
  66.  
  67. private
  68. PROCEDURE WriteFixedDocEnd (STRING dir)
  69. {
  70.   STRING str = "</FixedDocument>\n";
  71.  
  72.   str.Write (filename: dir + "/FixedDocument.fdoc", mode:"a", value:1);
  73. }
  74.  
  75. private
  76. PROCEDURE mapHex (INTEGER num)
  77.   RETURNS (STRING hex)
  78. {
  79.   STRING h = "0123456789ABCDEF";
  80.   INTEGER v1 = num / 16;
  81.   INTEGER v2 = num - v1 * 16;
  82.  
  83.   hex = h.index (val:v1) + h.index (val:v2);
  84. }
  85.  
  86. private
  87. PROCEDURE mapExt (XIPIMAGE img, INTEGER layer, INTEGER isTIFF)
  88.   RETURNS (STRING ext)
  89. {
  90.   ext = "unknown";
  91.  
  92.   INTEGER ltype = img.getMember (num:layer, member:"layerType");
  93.   INTEGER comp  = img.getMember (num:layer, member:"compression");
  94.  
  95.   if ( ltype == XIP_Contone || comp == XIP_JPEG_COMP) {
  96.     ext = "jpg";
  97.   } else {
  98.     ext = "png";
  99.     if (isTIFF && ltype != XIP_ColorMask) ext = "tif";
  100.   }
  101. }
  102.  
  103. private
  104. PROCEDURE makeViewboxValue (XIPIMAGE img, INTEGER layerNum)
  105.   RETURNS (STRING value) {
  106.    DOUBLE  xres, yres;
  107.  
  108.    DOUBLE pix = img.getMember (num:layerNum, member:"pixels");
  109.    DOUBLE scl = img.getMember (num:layerNum, member:"scanlines");
  110.  
  111.    xres = img.getMember (num:layerNum, member:"xres");
  112.    yres = img.getMember (num:layerNum, member:"yres");
  113.  
  114.    if (xres == 0.0)
  115.       xres = 96.0;
  116.  
  117.    if (yres == 0.0)
  118.       yres = 96.0;
  119.  
  120.    DOUBLE w = pix * 96.0 / xres;
  121.    DOUBLE h = scl * 96.0 / yres;
  122.  
  123.    value = "\"0,0," + w + "," + h + "\"";
  124. }
  125.  
  126. PROCEDURE makeViewportValue (LIST win) RETURNS (STRING value)
  127. {
  128.    value = "\"" + win[0] + "," + win[1] + "," + win[2] + "," + win[3] + "\"";
  129. }
  130.  
  131. PROCEDURE makePathDataValue (LIST viewport) RETURNS (STRING value)
  132. {
  133.    DOUBLE x, y, w, h;
  134.    x = viewport[0];
  135.    y = viewport[1];
  136.    w = viewport[2];
  137.    h = viewport[3];
  138.  
  139.    value = x + "," + y + " L " +
  140.            (x+w) + "," + y + " " +
  141.            (x+w) + "," + (y+h) + " " +
  142.            x + "," + (y+h);
  143. }
  144.  
  145. /* Arguments are:
  146. ** The Page Image
  147. ** The file name we're writing to
  148. ** The bounding box X position
  149. ** The bounding box Y position
  150. ** spi / resolution x
  151. ** spi / resolution y
  152. ** The page number
  153. ** OCR data if we have it
  154. ** Boolean for are we writing TIFF files
  155. */
  156. private
  157. PROCEDURE
  158. WriteFixedPgLayers (XIPIMAGE img, STRING file,
  159.                     DOUBLE bx, DOUBLE by,
  160.                     DOUBLE fx, DOUBLE fy,
  161.                     INTEGER page, STRING ocrxml, INTEGER isTIFF)
  162. {
  163.     INTEGER     ltype, lcomp, i, irot;
  164.     DOUBLE      xres, yres;
  165.     DOUBLE      ww=img.imageWidth, hh=img.imageHeight, w, h, xpos, ypos, rot, cos, sin;
  166.     STRING      brush, ext, str, stroke; // ="Stroke=\"#0000ff\""  // debugging;
  167.     STRING      xform_str, path_data_str, viewbox_str, viewport_str;
  168.     XIPCOLOR    xipcolor;
  169.     LIST        xform, viewport, window;
  170.     TRANSFORM2D matrix, rotmatrix;
  171.  
  172.     /*
  173.     ** Get orthogonal page rotation. Note that XPS uses CW rotation for
  174.     ** positive angles. Our document model defines CCW direction for the
  175.     ** rotation of positive angles. To conform to the document model, the
  176.     ** orthogonal page rotation angle must be negated.
  177.     */
  178.     rot = img.getAttr (name:"_PDFrotate");
  179.     rot = -1 * rot;
  180.     if (rot != 0) {
  181.       window = (0.0, 0.0, img.imageWidth, img.imageHeight);
  182.       rotmatrix.translate (tx: -1 * window[2] / 2.0, ty: -1 * window[3] / 2.0);
  183.       rotmatrix.rotate (angle: rot);
  184.       rotmatrix.applyToWin (window: window);
  185.       rotmatrix.translate (tx: window[2] / 2.0, ty: window[3] / 2.0);
  186.       }
  187.  
  188.     for ( i=0; i<img.nlayers; i++ )
  189.       {
  190.       ltype = img.getMember (num:i, member:"layerType");
  191.       if ( ltype == XIP_JBIG2Dict || ltype == XIP_Thumbnail)
  192.         continue;
  193.  
  194.       xpos = bx + img.getMember (num:i, member:"xpos") * fx;
  195.       ypos = by + img.getMember (num:i, member:"ypos") * fy;
  196.       w    = img.getMember (num:i, member:"width") * fx;
  197.       h    = img.getMember (num:i, member:"height") * fy;
  198.       rot  = img.getMember (num:i, member:"rotation");
  199.       irot = rot;
  200.       cos  = rot.cos();
  201.       sin  = rot.sin();
  202.       ext  = mapExt (img:img, layer:i, isTIFF: isTIFF);
  203.  
  204.       /* this clears very small numbers */
  205.       if (cos.abs() < .00001) cos = 0;
  206.       if (sin.abs() < .00001) sin = 0;
  207.  
  208.       // Create viewbox value for current layer
  209.       viewbox_str = makeViewboxValue (img: img, layerNum: i);
  210.  
  211.       // Create matrix transform value
  212.       xform = img.getAttr (num:i, name:"_DIOMatrixTransform");
  213.       if (xform) {
  214.         xform[4] *= fx;
  215.         xform[5] *= fy;
  216.         }
  217.       else if ( rot != 0  && ltype == XIP_Text) {
  218.         // temporary hack until transforms are fixed for Text layers
  219.         xform = (cos, -sin, sin, cos, xpos, ypos);
  220.         }
  221.       else {
  222.         xform = (1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
  223.         }
  224.  
  225.       matrix.setCoeffs (coeffs: xform);
  226.       matrix.concatenate (other: rotmatrix);
  227.       xform_str = "\"" + matrix.toString () + "\"";
  228.  
  229.       viewport = (xpos, ypos, w, h);
  230.  
  231.       matrix.invert ();
  232.       viewport     = matrix.applyToWin (window: viewport);
  233.       viewport_str = makeViewportValue (win: viewport);
  234.  
  235.       path_data_str = makePathDataValue (viewport: viewport);
  236.  
  237.       if ( ltype == XIP_Contone || ltype == XIP_Binary )
  238.         {
  239.         str = "<Canvas>\n" +
  240.         "  <Canvas.RenderTransform>\n" +
  241.         "    <MatrixTransform Matrix=" + xform_str +"/>\n" +
  242.         "  </Canvas.RenderTransform>\n" +
  243.         "  <Path " + stroke + " Data=\"M "+ path_data_str +" Z\">\n" +
  244.         "    <Path.Fill>\n" +
  245.         "      <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
  246.         "       ViewboxUnits=\"Absolute\"\n" +
  247.         "       Viewbox=" + viewbox_str + "\n"+
  248.         "       ViewportUnits=\"Absolute\"\n" +
  249.         "       Viewport=" + viewport_str +"/>\n" +
  250.         "    </Path.Fill>\n" +
  251.         "  </Path>\n" +
  252.         "</Canvas>\n";
  253.         }
  254.  
  255.       else if ( ltype == XIP_Text )
  256.         { 
  257.         STRING vis = "\"0.0\"";
  258.         if ( "$ESIPS_TEST_ENV" ) vis = "\"1.0\"";
  259.         str = "<Canvas" + " Opacity = " + vis + " RenderTransform=" + xform_str + ">\n";
  260.         str.Write (filename: file, value:1, mode:"a");
  261.         ocrxml.Write (filename: file, value:1, mode:"a");
  262.         str = "</Canvas>\n";
  263.         }
  264.  
  265.       else if ( ltype == XIP_ColorMask )
  266.         {
  267.         xipcolor = img.getMember(num:i, member:"color");
  268.         if ( xipcolor ) {
  269.           if ((xipcolor.photometry == XIP_SGRAY_COLOR) || (xipcolor.photometry == XIP_GRAY_COLOR))
  270.             brush = "#" + mapHex(num: xipcolor.c[0]) + mapHex(num: xipcolor.c[0]) + mapHex(num: xipcolor.c[0]);
  271.           else
  272.             brush = "#" + mapHex(num: xipcolor.c[0]) + mapHex(num: xipcolor.c[1]) + mapHex(num: xipcolor.c[2]);
  273.           }
  274.         else
  275.           brush = "#000000";
  276.  
  277.         str = "<Canvas>\n" +
  278.         "  <Canvas.RenderTransform>\n" +
  279.         "    <MatrixTransform Matrix=" + xform_str + "/>\n" +
  280.         "  </Canvas.RenderTransform>\n" +
  281.         "  <Path " + stroke + " Fill=\""+brush+"\" "+"Data=\"M "+ path_data_str +" Z\">\n" +
  282.         "    <Path.OpacityMask>\n" +
  283.         "      <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
  284.         "        ViewboxUnits=\"Absolute\"\n" +
  285.         "        Viewbox=" + viewbox_str + "\n"+
  286.         "        ViewportUnits=\"Absolute\"\n" +
  287.         "        Viewport=" + viewport_str + "/>\n" +
  288.         "    </Path.OpacityMask>\n" +
  289.         "  </Path>\n" +
  290.         "</Canvas>\n";
  291.         }
  292.     
  293.       else if ( ltype == XIP_ContoneMask )
  294.         {
  295.         str = "<Canvas RenderOptions.EdgeMode=\"Aliased\">\n" +
  296.         "  <Canvas.RenderTransform>\n" +
  297.         "    <MatrixTransform Matrix=" + xform_str +"/>\n" +
  298.         "  </Canvas.RenderTransform>\n" +
  299.         "  <Path " + stroke + " Data=\"M "+ path_data_str +" Z\">\n" +
  300.         "    <Path.OpacityMask>\n" +
  301.         "      <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
  302.         "       ViewboxUnits=\"Absolute\"\n" +
  303.         "       Viewbox="+ viewbox_str + "\n"+
  304.         "       ViewportUnits=\"Absolute\"\n" +
  305.         "       Viewport=" + viewport_str + "/>\n" +
  306.         "    </Path.OpacityMask>\n";
  307.    
  308.         i++; // Get the next Layer 
  309.         xpos = bx + img.getMember (num:i, member:"xpos") * fx;
  310.         ypos = by + img.getMember (num:i, member:"ypos") * fy;
  311.         w    = img.getMember (num:i, member:"width") * fx;
  312.         h    = img.getMember (num:i, member:"height") * fy;
  313.         ext  = mapExt (img:img, layer:i, isTIFF: isTIFF);
  314.  
  315.         // Create viewbox value for next layer
  316.         viewbox_str = makeViewboxValue (img: img, layerNum: i);
  317.  
  318.         viewport = (xpos, ypos, w, h);
  319.  
  320.         viewport     = matrix.applyToWin (window: viewport);
  321.         viewport_str = makeViewportValue (win: viewport);
  322.  
  323.         str  = str +
  324.         "    <Path.Fill>\n" +
  325.         "      <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
  326.         "       ViewboxUnits=\"Absolute\"\n" +
  327.         "       Viewbox=" + viewbox_str + "\n"+
  328.         "       ViewportUnits=\"Absolute\"\n" +
  329.         "       Viewport=" + viewport_str + "/>\n" +
  330.         "    </Path.Fill>\n" +
  331.         "  </Path>\n" +
  332.         "</Canvas>\n";
  333.         }
  334.  
  335.       str.Write (filename: file, value:1, mode:"a");
  336.       }
  337. }
  338.  
  339.  
  340. private
  341. PROCEDURE WriteFixedPg (XIPIMAGE img, STRING dir, INTEGER page, STRING ocrxml,
  342.                         DOUBLE page_x, DOUBLE page_y, INTEGER unit, INTEGER isTIFF)
  343. {
  344.   DOUBLE fx = 96 / img.resolution[0];
  345.   DOUBLE fy = 96 / img.resolution[1];
  346.   DOUBLE w = img.imageWidth * fx;
  347.   DOUBLE h = img.imageHeight * fy;
  348.   DOUBLE pw = w;  // Page width
  349.   DOUBLE ph = h;  // Page height
  350.  
  351.   // Base unit is spots per inch
  352.   if ( unit ) {
  353.     pw = 96 * page_x; 
  354.     ph = 96 * page_y;
  355.     if ( unit == 2 ) { // millimeters
  356.       pw = pw / 25.4;   
  357.       ph = ph / 25.4;
  358.       }
  359.     }
  360.  
  361.   STRING file = dir + "/" + page + ".fpage";
  362.   STRING ext = mapExt (img:img, layer:0, isTIFF: isTIFF);
  363.  
  364.   STRING str =
  365.     "<FixedPage " + "Width=\""  + pw + "\" " + "Height=\"" + ph + "\" " +
  366.     "xmlns=\"http://schemas.microsoft.com/xps/2005/06\" " +
  367.     "xml:lang=\"en-US\">\n";
  368.   str.Write (filename: file, value:1);
  369.  
  370.   DOUBLE bx = pw/2 - w/2;  // Bounding box X edge
  371.   DOUBLE by = ph/2 - h/2;  // Bounding box Y edge
  372.   WriteFixedPgLayers (img:img, file:file, bx:bx, by:by, fx:fx, fy:fy, page:page, ocrxml:ocrxml, isTIFF: isTIFF);
  373.  
  374.   str = "</FixedPage>\n";
  375.   str.Write (filename: file, value:1, mode:"a");
  376. }
  377.  
  378.  
  379.  
  380. private
  381. PROCEDURE WriteRelsFixedPg (XIPIMAGE img, STRING dir, INTEGER page, STRING thumbName, STRING font, INTEGER isTIFF)
  382. {
  383.    STRING filename = dir + "/" + page + ".fpage.rels";
  384.    INTEGER i, ltype;
  385.    INTEGER tid = -1;
  386.  
  387.    STRING str = 
  388.    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  389.    "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n"; 
  390.    str.Write (filename: filename, value:1);
  391.  
  392.    for ( i=0; i<img.nlayers; i++ ) {
  393.       ltype = img.getMember (num:i, member:"layerType");
  394.       if ( ltype == XIP_JBIG2Dict) {
  395.              continue;
  396.       } else if ( ltype == XIP_Thumbnail) {
  397.          // remember the layer index. Thumbnail should be written at the end 
  398.          tid = i;
  399.       } else if ( ltype == XIP_Text) {
  400.           if (!font)
  401.              continue;
  402.           str = "<Relationship Target=\"/Fonts/" + font +
  403.              "\" Id=\"R"+ i +"\" " +
  404.              "Type=\"http://schemas.microsoft.com/xps/2005/06/required-resource\"/>\n";
  405.           str.Write (filename: filename, value:1, mode:"a");
  406.       } else {
  407.           str = "<Relationship Target=\"/Images/pg"+page+"Layer" + i + "." +
  408.              mapExt (img:img, layer:i, isTIFF: isTIFF) + "\" Id=\"R"+ i +"\" " +
  409.              "Type=\"http://schemas.microsoft.com/xps/2005/06/required-resource\"/>\n";
  410.           str.Write (filename: filename, value:1, mode:"a");
  411.         }
  412.    }
  413.    if (tid >=0) {
  414.         str = "<Relationship Target=\"" + thumbName + "\" Id=\"R"+ i + "\" " +
  415.           "Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail\"/>\n";
  416.        str.Write (filename: filename, value:1, mode:"a");
  417.    }
  418.    str =  "</Relationships>\n";
  419.     str.Write (filename: filename, value:1, mode:"a");
  420. }
  421.  
  422. private
  423. PROCEDURE WriteRelsDotRels (STRING dir, STRING thumbName)
  424. {
  425.   STRING fname = "FixedDocumentSequence.fdseq";
  426.   STRING filename = dir + "/.rels";
  427.  
  428.   STRING str = 
  429.    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  430.    "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n" +
  431.     "<Relationship Target=\"/" + fname + "\" Id=\"R0\" Type=\"" +
  432.    "http://schemas.microsoft.com/xps/2005/06/fixedrepresentation\"/>\n" +
  433.    "<Relationship Target=\"/core.xml\" Id=\"R1\" " +
  434.    "Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\"/>\n";
  435.     str.Write (filename: filename, value:1);
  436.  
  437.    if (thumbName != "") {
  438.       str = "<Relationship Target=\"" + thumbName + "\" Id=\"R2\" " + 
  439.       "Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail\"/>\n";
  440.        str.Write (filename: filename, value:1, mode:"a");
  441.    }
  442.    str =  "</Relationships>\n";
  443.     str.Write (filename: filename, value:1, mode:"a");
  444.  
  445. }
  446.  
  447. private
  448. PROCEDURE WriteDocSeqRels (STRING dir)
  449. {
  450.   STRING str = 
  451.    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  452.    "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n" +
  453.    "</Relationships>\n";
  454.  
  455.   str.Write (filename: dir + "/FixedDocumentSequence.fdseq.rels", value:1);
  456. }
  457.  
  458. private
  459. PROCEDURE WriteMetaData (STRING dir, STRING creator, STRING title, STRING keyword, STRING dateTime)
  460. {
  461.   STRING str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  462.                "<coreProperties xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " +
  463.             "xmlns:dcterms=\"http://purl.org/dc/terms/\" " +
  464.             "xmlns=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" " +
  465.             "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> \n" +
  466.                "<dc:title>" + title + "</dc:title><dc:subject></dc:subject><dc:creator>" + creator + 
  467.             "</dc:creator><dcterms:created xsi:type=\"dcterms:W3CDTF\">" + dateTime + 
  468.             "</dcterms:created><dcterms:modified xsi:type=\"dcterms:W3CDTF\">" + dateTime + 
  469.             "</dcterms:modified><keywords>" + keyword + "</keywords></coreProperties>\n";
  470.  
  471.   str.Write (filename: dir + "/core.xml", value:1);
  472. }
  473.